<?php
/**
*  会员课程提问
*/
namespace app\api\model;
use think\Model;

class MemberCourseQuestion extends Model
{
    /**
    *  列表
    */
    public function _list($where = '', $order_by="created desc", $page_size = PAGE_SIZE_API){
        $obj = $this->order($order_by);
        $data = $this->alias('mcq')->where($where)
            ->field('mcq.*, cm.title')
            ->join("course_menu cm", 'cm.id=mcq.cmid', 'LEFT')
            ->order($order_by)
            ->paginate($page_size)
            ->toArray();
        return $data;
    }

    /**
     *  列表
     */
    public function _questionDetail($where){
        $data = $this->alias('mcq')->where($where)
            ->field('mcq.*, cm.title')
            ->join("course_menu cm", 'cm.id=mcq.cmid', 'LEFT')
            ->find();
        return $data;
    }
    
    
    /**
    * 问题
    */
    public function _questionList($data){
        if(empty($data['mid'])){
            $this->error = '会员id缺失';
            return false;
        }
        $mid = (int)$data['mid'];
        $where = ['mcq.mid'=>$mid];
        if(!empty($data['cid'])){
            $where['mcq.cid'] = $data['cid'];
        }
        if(isset($data['status'])){
             $where['mcq.status'] = $data['status'];
        }
        
        return $this->_list($where);
        
    }
    
    /**
    * 提问
    */
    public function _create($data){
        if(empty($data['content'])){
            $this->error = '请输入内容';
            return false;
        }
       
        if(empty($data['cmid'])){
            $this->error = '章节ID缺失';
            return false;
        }

        $data['created'] = time();

        $ret = $this->save($data);
        if(!$ret){
            $this->error = '提交失败';
            return false;
        }
        return true;
    }
    
    
    /**
    * 通过id获取信息
    */
    public function _getInfoByID($id){
        return $this->where([['id', '=',$id]])->find();
    }
    
    /**
    * 通过条件获取信息
    */
    public function _getInfoByWhere($where){
        return $this->where($where)->find();
    }


}